//package com.wxy.demo.config;
//
//import com.alibaba.fastjson.JSON;
//import com.fasterxml.jackson.core.JsonEncoding;
//import com.fasterxml.jackson.core.JsonGenerator;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.core.util.DefaultIndenter;
//import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
//import com.fasterxml.jackson.databind.*;
//import com.fasterxml.jackson.databind.ser.FilterProvider;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.http.HttpOutputMessage;
//import org.springframework.http.MediaType;
//import org.springframework.http.converter.HttpMessageNotWritableException;
//import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
//import org.springframework.http.converter.json.MappingJacksonValue;
//import org.springframework.util.TypeUtils;
//
//import java.io.IOException;
//import java.lang.reflect.Type;
//
///**
// * @author: Jack
// * 2020-08-24 23:10
// */
//@Slf4j
//public class OvsHttpMessageConverter extends MappingJackson2HttpMessageConverter {
//
//    private static final MediaType TEXT_EVENT_STREAM = new MediaType("text", "event-stream");
//
//
//    public OvsHttpMessageConverter() {
//    }
//
//    public OvsHttpMessageConverter(ObjectMapper objectMapper) {
//        super(objectMapper);
//    }
//
//    @Override
//    protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
//            throws IOException, HttpMessageNotWritableException {
//
//        log.info("OvsHttpMessageConverter writeInternal,type={},object={}", type.getTypeName(), object);
//
//        MediaType contentType = outputMessage.getHeaders().getContentType();
//        JsonEncoding encoding = getJsonEncoding(contentType);
//        JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
//
//        try {
//            Object value = object;
//            Class<?> serializationView = null;
//            FilterProvider filters = null;
//            JavaType javaType = null;
//
//            if (object instanceof MappingJacksonValue) {
//                MappingJacksonValue container = (MappingJacksonValue) object;
//                value = container.getValue();
//                serializationView = container.getSerializationView();
//                filters = container.getFilters();
//            }
//
//            ObjectWriter objectWriter = (serializationView != null ? this.objectMapper.writerWithView(serializationView) : this.objectMapper.writer());
//
//            // 对于String类型的，直接 append 返回，不转json
//            if ("java.lang.String".equals(type.getTypeName())) {
//
//                try {
//                    // 1.先解析json對象,如果不是json對象的,走catch邏輯
//                    Object jsonObject = JSON.parse(value.toString());
//                    objectWriter.writeValue(generator, jsonObject);
//                } catch (Throwable e) {
//                    log.error("OvsHttpMessageConverter writeInternal,JSON.parse(value.toString()) = {}", value, e);
//                    // 2.不是json對象的,就原樣輸出string
//                    objectWriter.writeValue(generator, value);
//                }
//
//            } else {
//
//                writePrefix(generator, object);
//
//                if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
//                    javaType = getJavaType(type, null);
//                }
//
//                if (filters != null) {
//                    objectWriter = objectWriter.with(filters);
//                }
//                if (javaType != null && javaType.isContainerType()) {
//                    objectWriter = objectWriter.forType(javaType);
//                }
//                SerializationConfig config = objectWriter.getConfig();
//                if (contentType != null && contentType.isCompatibleWith(TEXT_EVENT_STREAM) &&
//                        config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
//
//                    DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
//                    prettyPrinter.indentObjectsWith(new DefaultIndenter("  ", "\ndata:"));
//                    objectWriter = objectWriter.with(prettyPrinter);
//                }
//                objectWriter.writeValue(generator, value);
//                writeSuffix(generator, object);
//            }
//
//            generator.flush();
//
//        } catch (JsonProcessingException ex) {
//            throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getOriginalMessage(), ex);
//        }
//    }
//
//}